package com.buba.matter.controller;

import com.buba.customer.pojo.Customer;
import com.buba.matter.pojo.Info;
import com.buba.matter.service.InfoService;
import com.buba.system.pojo.Page;
import com.buba.util.GenerateKey;
import com.buba.util.MyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Info")
public class InfoController {
    private InfoService infoService;

    /**
     * 查询物质明细信息
     * @param map
     * @return
     */
    @RequestMapping("/selInfoTable")
    public Page selInfoTable(@RequestParam Map<String, Object> map){
        Page<Info> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        String itemsName = map.get("itemsName").toString();
        List<Info> list = null;
        // 没有条件查询
        if ("".equals(itemsName) ){
            //查询物质明细总条数
            page.computePageTotal(infoService.selInfoCount());
            //查询所有物质明细
            list = infoService.selInfoTable(page);
        }else { // 携带查询条件
            page.computePageTotal(infoService.selCount(map));
            list = infoService.selAllInfo(map);
        }
        //把list装入page类
        page.setList(list);
        return page;
    }
    /**
     * 添加物质明细信息
     * @RequestParam
     * @return
     */
    @RequestMapping("/addInfo")
    public Map<String,Object> addInfo(@RequestParam Map<String, Object> map){
        //指定格式开头加上当前的事件作为id 调用时间戳类
        String itemsInfoNo = GenerateKey.getKey("MI");
        map.put("itemsInfoNo",itemsInfoNo);
        //1、添加客户信息 返回添加的条数
        Integer count = infoService.addInfo(map);
        if(count>0){
            map.put("message","添加成功");
        }else {
            map.put("message","添加失败");
        }
        return map;
    }
    /**
     * 删除物质明细信息
     * @param
     * @return
     */
    @RequestMapping("/delInfoAll")
    public Map<String,Object> delInfoAll(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer delInfo = infoService.delInfoAll(list);
        if (delInfo != null){
            map.put("message","删除"+delInfo+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }
    /**
     * 修改物质明细信息
     * @param map
     * @return
     */
    @RequestMapping("/updInfo")
    public Map<String, Object> updInfo(@RequestParam Map<String, Object> map) {
        Integer updCount = infoService.updInfo(map);
        return map;
    }

    /**
     * 导出excel方法
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        Map<String,Object> map = new HashMap<>();
        //查询客户信息
        List<Info> matterInfo = infoService.selMatterInfoByCondition(map);
        new MyExcel<Info>().exportExcel("货物明细表",matterInfo,Info.class,response);
    }

    /**
     * 导入excel方法
     * @param upFile
     * @throws Exception
     */
    @RequestMapping("/importExcel")
    public void importExcel(@RequestPart("upFile") MultipartFile upFile) throws Exception {
        List<Info> list = new MyExcel<Info>().importExcel(upFile, Info.class);
        Long count = infoService.addMatterInfoByList(list);
    }
    /**
     * 查询id
     * @return
     */
    @RequestMapping("/selMatterInfoItemsInfoNo")
    public Map<String, Object> selMatterInfoItemsInfoNo(){
        Map<String, Object> map = new HashMap<>();
        List<Info> list = infoService.selMatterInfoItemsInfoNo(map);
        map.put("list",list);
        return map;
    }

    @Autowired
    public void setInfoService(InfoService infoService) {
        this.infoService = infoService;
    }
}
